package de.dhbw.horb.inf2013.vierFarbenProblem.viewModel.lineToFigure;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * The Class Figure (not working).
 */
public class Figure {

	/** The lines. */
	private List<Line> lines = new LinkedList<Line>();

	/**
	 * Adds the line.
	 * 
	 * @param l
	 *            the l
	 */
	public void addLine(Line l) {
		this.lines.add(l);
	}

	/**
	 * Contains line.
	 * 
	 * @param l
	 *            the l
	 * @return true, if successful
	 */
	public boolean containsLine(Line l) {
		return this.lines.contains(l);
	}

	/**
	 * Checks if is figure.
	 * 
	 * @return true, if is figure
	 */
	public boolean isFigure() {
		if (lines.size() >= 3) {

			List<Line> circle = new LinkedList<Line>();
			circle.add(lines.get(0));
			Point startPoint = lines.get(0).getFrom();

			outerloop: for (int i = 0; i < lines.size() - 1; i++) {
				for (int j = 0; j < circle.size() - 1; j++) {
					System.out.println("outerloop");
					System.out.println(i);
					if (circle.get(j).getTo().equals(startPoint)) {
						break outerloop;
					}

					if (circle.get(j).getTo().equals(lines.get(i).getFrom())) {
						circle.add(lines.get(i));
					}
				}
			}

			if (circle.size() == lines.size()
					&& circle.get(0).getFrom()
							.equals(circle.get(circle.size() - 1).getTo())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Checks if is equal.
	 * 
	 * @param obj
	 *            the obj
	 * @return true, if is equal
	 */
	public boolean isEqual(Object obj) {
		System.out.println("is equal Figure");
		Figure figure = (Figure) obj;
		boolean result = true;
		Iterator<Line> iterator = lines.iterator();

		while (iterator.hasNext()) {
			if (!figure.containsLine(iterator.next())) {
				result = false;
			}
		}
		return result;
	}
}
